# analysis/product_analysis.py
from pyspark.sql import DataFrame


class ProductAnalyzer:
    @staticmethod
    def analyze_weekday_pattern(df: DataFrame) -> DataFrame:
        return (df
                .groupBy("weekday")
                .agg({"visits": "sum",
                      "bounce_rate": "avg",
                      "conversion_rate": "avg"})
                .withColumnRenamed("sum(visits)", "total_visits")
                .withColumnRenamed("avg(bounce_rate)", "avg_bounce_rate")
                .withColumnRenamed("avg(conversion_rate)", "avg_conversion_rate"))

    @staticmethod
    def analyze_product_by_device(df: DataFrame, product_df: DataFrame) -> DataFrame:
        return (df
                .join(product_df, df.product == product_df.product)
                .groupBy("product", "device_type")
                .agg({"count": "sum"})
                .withColumnRenamed("sum(count)", "appearance_count")
                .orderBy("appearance_count", ascending=False))